import { HTTP } from '../utils/http-p.js'
import {config} from "../config.js"

class LoginModel extends HTTP{

  login(userInfo){
    wx.login({
      success: function (login_res) {
        wx.getUserInfo({
          success: function (res) {
            wx.request({
              url: config.api_blink_url+'/v/wx/login',
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                code:login_res.code,
                avatarUrl: userInfo.avatarUrl,
                nickname: userInfo.nickName,
                gender: userInfo.gender,
              },
              success:function(result) {
                console.log(result)
                if (result.data.code == 0) {
                  const token = result.data.data;
                  console.log(token)
                  // 将返回的数据保存到全局的缓冲中，方便其他页面使用
                  wx.setStorage({ key: 'token', data: token })
                }else {
                  wx.showToast({
                    title: '抱歉出现了一个错误',
                    icon:'none',
                    duration:2000
                  })
                }

              }
            })
          }
        })
      }
    })
  }
}

export { LoginModel}
